package com.mew.slots.core.game.ald.room;


import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.GameUtil;
import com.mew.common.util.NumberUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

public class AldRoom extends BaseRoom {

    public static int _SCATTER = 12;//_WILD不允许替换

    public int[] bounsWights = {2500,2500,2500,1500,200,5,1};
    public int[] bounsGolds = {20,30,40,50,80,100,150};

    public int[] jpWights = {99830,10,5,3};

    public AldRoom(){
        res = new AldPlayMsgs();
        row = 4;
        col = 6;
        _WILD = 11;
        spes = new ArrayList<Integer>(){{
            add(12);
        }};
        // 9 0, 10  1, j  2, q  3  ,k  4,  a  5, 鸟  6  ，猴  7 ，虎  8 ，女  9，  男  10， W 11, S  12

         rate = new int[][]{
                {0, 0,  10,   20, 30,40},
                {0, 0,  10,   20, 30,40},
                {0, 0,  10,   20, 30,40},
                {0, 0,  10,   20, 30,40},
                {0, 0,  15,   30, 45,60},
                {0, 0,  15,   30, 45,60},
                {0, 0,  20,   40, 60,100},
                {0, 0,  20,   40, 60,100},
                {0, 0,  20,   40, 60,100},
                {0, 0,  30,   60,100,150},
                {0, 0,  30,   60,100,150},
                {0, 0,  50,   100, 150,  200},
                {0, 0,  0,   0, 0, 0},

        };

         alllines = new int[][]{
                 {6,7,8,9,10,11},
                 {12,13,14,15,16,17},
                 {0,1,2,3,4,5},
                 {18,19,20,21,22,23},
                 {18,13,8,9,16,23},
                 {0,7,14,15,10,5},
                 {12,7,2,3,10,17},
                 {6,13,20,21,16,11},
                 {18,13,20,21,16,23},
                 {0,7,2,3,10,5},
                 {12,19,14,15,22,17},
                 {6,1,8,9,4,11},
                 {12,7,14,15,10,17},
                 {6,13,8,9,16,11},
                 {18,19,14,15,22,23},
                 {0,1,8,9,4,5},
                 {12,13,8,9,16,17},
                 {6,7,14,15,10,11},
                 {6,7,2,3,10,11},
                 {12,13,20,21,16,17},
                 {6,13,14,15,16,11},
                 {12,7,8,9,10,17},
                 {12,19,20,21,22,17},
                 {6,1,2,3,4,11},
                 {0,7,8,9,10,5},
                 {18,13,14,15,16,23},
                 {6,19,20,21,22,11},
                 {12,1,2,3,4,17},
                 {0,13,14,15,16,5},
                 {18,7,8,9,10,23},
                 {6,7,20,21,10,11},
                 {12,13,2,3,16,17},
                 {0,1,14,15,4,5},
                 {18,19,8,9,22,23},
                 {0,1,20,21,4,5},
                 {18,19,2,3,22,23},
                 {18,13,8,3,4,5},
                 {0,7,14,21,22,23},
                 {0,1,2,9,16,23},
                 {18,19,20,15,10,5},
                 {12,7,2,3,4,5},
                 {6,13,20,21,22,23},
                 {0,1,2,3,10,17},
                 {18,19,20,21,16,11},
                 {18,13,8,9,10,11},
                 {0,7,14,15,16,17},
                 {6,7,8,9,16,23},
                 {12,13,14,15,10,5},
                 {6,19,2,3,22,11},
                 {12,1,20,21,4,17}
        };
    }


    @Override
    public void freeBouns(){
        //free
        int fsdb = fsdb(_SCATTER);
        if (fsdb==3){
            res.free = 1;
            res.freeNumber = 10;
        }else if (fsdb==4){
            res.free = 1;
            res.freeNumber = 10;
        }else if (fsdb==5){
            res.free = 1;
            res.freeNumber = 10;
        }else if (fsdb==6){
            res.free = 1;
            res.freeNumber = 10;
        }

        //jp
        int jpw = controlType==4?2:GameUtil.weights(jpWights);
        res.jp = jpw;

        if (jpw==0){
            ((AldPlayMsgs)res).jpStar = RandomUtil.randomInt(0,4);
            return;
        }

        if (jpw==1){
            ((AldPlayMsgs)res).jpStar = RandomUtil.randomInt(5,8);
            return;
        }

        if (jpw==2){
            ((AldPlayMsgs)res).jpStar = RandomUtil.randomInt(9,11);
            return;
        }
        if (jpw==3){
            ((AldPlayMsgs)res).jpStar = 12;
            return;
        }

    }

    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts( free ? 1 : 0,rollerID);
        }

        if (free){
            endSlots[5] = _WILD;
            endSlots[11] = _WILD;
            endSlots[17] = _WILD;
            endSlots[23] = _WILD;
        }
        AldPlayMsgs aldres = (AldPlayMsgs) res;
        aldres.startSlots = endSlots.clone();

        //行W变化
        for (int i = 0; i < row; i++) {
            int s = -1;
            int e = -1;
            for (int j = i*col ; j < (i+1)*col; j++) {
                if (s==-1&&endSlots[j]==_WILD){
                    s = j;
                }
                if (s>-1&&endSlots[j]==_WILD){
                    e = j;
                }
            }

            //w之间变成W
            for (int j = s+1; j < e; j++) {
                endSlots[j]=_WILD;
                res.bouns = 1;//出了特殊
            }
        }

        if (res.bouns == 1||controlType==3){
            if (RandomUtil.randomInt(100)<10||controlType==3){

                int weights = GameUtil.weights(bounsWights);
                //几个向上
                ((AldPlayMsgs)res).up = weights ;
                res.bounswin = bounsGolds[weights]* res.bet*res.line;
            }else {
                res.bouns = 0;
            }
        }

        res.endSlots = endSlots.clone();
    }

    /**
     *  每条线算分
     */
    @Override
    public void checkaward(){

        int[] rl;
        int type;
        int num;

        for (int j = 0;j< iconLines.length;j++) {

            BasePlayMsgs.LineResult bgo = new BasePlayMsgs.LineResult();
            bgo.lineNum = j;
            rl = iconLines[j].clone();
            type = rl[0];
            num = 0;

            //w算分

            BasePlayMsgs.LineResult wbgo = new BasePlayMsgs.LineResult();
            if (type==_WILD){
                for (int i = 0; i < rl.length; i++) {
                    if (rl[i] == _WILD) {
                        num++;
                    }else {
                        break;
                    }
                }

                if (num>1){
                    Integer rate0 = rate[_WILD][num - 1];
                    if (rate0 > 0) {
                        wbgo.mult = rate0;
                        wbgo.type = _WILD;
                        wbgo.num = num;
                        wbgo.uvIdx = Arrays.stream(lines[j]).limit(num).toArray();

                    }
                }
            }

            //普通图标
            num = 0;
            //确定类型
            for(int i=0;i<rl.length;i++){
                if(rl[i]!=_WILD){
                    type=rl[i];
                    break;
                }
            }

            //不为特殊图标
            if (spes.contains(type)){
                continue;
            }

            for (int i = 0; i < rl.length; i++) {
                if (type==rl[i]||rl[i] == _WILD) {
                    num++;
                }else {
                    break;
                }
            }

            if (num>1){
                Integer rate0 = rate[type][num - 1];
                if (rate0 > 0) {
                    bgo.mult = rate0;
                    bgo.type = type;
                    bgo.num = num;
                    bgo.uvIdx = Arrays.stream(lines[j]).limit(num).toArray();

                }
            }

            if (wbgo.getMult()>bgo.getMult()){
                res.lines.add(wbgo);
            }else if (bgo.getMult()>0){
                res.lines.add(bgo);
            }

        }

        for(BasePlayMsgs.LineResult x:res.lines)
        {
            res.multiple += x.mult;
        }

    }


    @Override
    public int[] control(){
        int[] e = rollerXml.getLineInts( free ? 1 : 0,1);
        if (controlType==2){
            e[0] = _SCATTER;
            e[2] = _SCATTER;
            e[4] = _SCATTER;
        }else {
            e[6] = _WILD;
            e[11] = _WILD;
        }
        return e;
    }

    public static void main(String[] args) {

        AldRoom room = new AldRoom();
        GameUtil.weights(room.bounsWights);
        long a = 0;
        for (int i = 0; i < 100000; i++) {
            a+= room.bounsGolds[GameUtil.weights(room.bounsWights)];
        }

        System.out.println(a/100000.0);
    }

}
